package edu.spsu.utils;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import edu.spsu.R;
import edu.spsu.SPSUActivity;
import edu.spsu.library.LibraryActivity;
import edu.spsu.mail.EmailListingActivity;
import edu.spsu.mail.EmailState;

public final class Util {
	
	public static int CURRENT_MODE = Log.INFO;
	private static SharedPreferences pref;
	public static int INBOX_MESSAGES = 20;
	
	/**
	 * The report method handles the info, debug and error messages for the application
	 * 
	 * @param context The Application Context (e.g, getContext())
	 * @param TAG The Activity that has sent the message (e.g, SPSUActivity)
	 * @param message The message that has been sent  (e.g, Class.toString())
	 * @param exception Is the message that has been sent an Exception?
	 * 
	 * */
	public static void report(Context context, String TAG, Object message, boolean exception) {
		
		if(exception)
		{
			showToast((Activity)context, "Exception: "+message);
			Log.e(TAG, ""+message);
		}
		else if(CURRENT_MODE == Log.DEBUG)
		{
			showToast((Activity)context, "Debug: "+message);
			Log.d(TAG, ""+message);
		}
		else
		{
			Log.i(TAG, ""+message);
		}

	}

	/**
	 * The showToast method sends Toast messages to the application's UI Thread
	 * 
	 * @param act The Application Context (e.g., view.getContext())
	 * @param message Message sent (e.g, Debug: onCreate completed successfully)
	 * */
	public static void showToast(final Activity act, final String message) {
		
		act.runOnUiThread(new Runnable() {
		    public void run() {
		        Toast.makeText(act, message, Toast.LENGTH_SHORT).show();
		    }
		});
	}
	
	/**
	 * The getPref method retrieves the application's Shared Preferences
	 * 
	 * @param context The Application Context (e.g., view.getContext())
	 * */
	public static SharedPreferences getPref(Context context) {
		if(pref == null) pref = PreferenceManager.getDefaultSharedPreferences(context);
		
		return pref;
	}
	
	/**
	 * The taskBarClick method handles task bar actions (e.g., Home, Email, etc.)
	 * @param view Provides Application Context and view id (e.g, view.getContext(), view.getId())
	 */
	public static void taskBarClick(View view) {
		
		final Context context = view.getContext();
		Intent intent = null;
		
		switch(view.getId())
		{
			case R.id.goHomeButton: intent = new Intent(context, SPSUActivity.class); break;
			case R.id.goToWeatherButton: intent = new Intent(context, edu.spsu.weather.WeatherListingActivity.class); break;
			case R.id.goToMailButton: intent = new Intent(context, EmailListingActivity.class); break;
			case R.id.goToLibraryButton: intent = new Intent(context, LibraryActivity.class); break;
			default: showToast((Activity)context, "This button has not been set up yet.");
		}
		
		if(intent != null)
		{
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			context.startActivity(intent);
		}
	}

	/**
	 * The setUpUser method provides the application a user and a password
	 * 
	 * @param context The Application Context (e.g., view.getContext())
	 * @param user The Application's user (e.g., cmiller3)
	 * @param pass The user's (encrypted?) password
	 * */
	public static void setUpUser(Context context, String user, String pass, boolean saveCredentials) {
		Editor ed  = getPref(context).edit();
		
		if(saveCredentials)
		{
			ed.putString("user", user);
			ed.putString("pass", pass);
		}
		else
		{
			ed.putString("user", "");
			ed.putString("pass", "");
		}
		
		ed.putBoolean("saveCredentials", saveCredentials);
		ed.putInt("inbox", INBOX_MESSAGES);
		ed.commit();
	}
	
	/**
	 * The connectToZimbra method will allow access to your Zimbra Inbox
	 * 
	 * @param context The Application Context (e.g., view.getContext())
	 * @param TAG The Application Activity that called (e.g., {@link EmailListingActivity})
	 * */
	public static void connectToZimbra(Context context, String TAG) {

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
			context, android.R.layout.simple_list_item_1, EmailState.getPrefaceArray());
		
		((EmailListingActivity)context).setListAdapter(adapter);
		
		if(!EmailState.isSetUp()) EmailState.setUpEmailState(context,TAG);

		if(!EmailState.connected()) EmailState.makeConnection();
		
	}
	
	/**
	 * The connectToZimbra method will allow access to your Zimbra Inbox
	 * 
	 * @param context The Application Context (e.g., view.getContext())
	 * @param TAG The Application Activity that called (e.g., {@link EmailListingActivity})
	 * */
	public static void connectToZimbra(Context context, String TAG, String user, String pass) {

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
			context, android.R.layout.simple_list_item_1, EmailState.getPrefaceArray());
		
		if(context instanceof ListActivity)((ListActivity)context).setListAdapter(adapter);
		
		if(!EmailState.isSetUp()) EmailState.setUpEmailState(context, TAG);
		
		if(!EmailState.connected()) EmailState.makeConnection(user, pass);
		
	}

	/**
	 * The getInboxMessages method retrieves messages from your Zimbra Inbox
	 * */
	public static void refreshInboxMessages() {
		if(EmailState.connected() && EmailState.messagesRecieved())
		{
			EmailState.retrieveInboxMessages(INBOX_MESSAGES);
		}
	}

}
